package com.zlkj.pro.core.annotation.impl;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 孙铭泽
 * @date 2020-10-28
 * @time 22:58
 **/
@Component
public class FileJsonSerializeImpl extends JsonSerializer<String> {

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        String filePath = ConfigContext.me().getConfigValueNullable("OSS_ALIYUN_OSS_DOMAIN_NAME", String.class);
        String[] split = value.split(",");
        StringBuilder sb = new StringBuilder();
        for (String s : split) {
            if (StrUtil.isNotBlank(s)) {
                if (!s.contains("https://")) {
                    sb.append(filePath).append("/").append(s).append(",");
                } else {
                    sb.append(s).append(",");
                }
            }
        }
        gen.writeString(StrUtil.strip(sb.toString(), ","));
    }
}
